package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController//将返回值转化为json
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * URL:/addUser
     * method:post
     * 参数：form表单传参
     * 返回值：返回成功的字符信息
     */
    @PostMapping ("/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "成功";
    }
    /**
     * 查询User表的全部数据
     * 请求路径：http;//localhost:8090/getUser
     * 参数：不需要参数
     * 返回值：list<User>
     */
    @GetMapping("/getUser")
    public List<User> getUser(){
        List<User> userList = userService.getUser();
        return userList;
    }

    /**
     * url:http://localhost:8080/get/servlet?id=100
     *  request 用户提交请求时的请求头信息
     *  response 服务器响应用户的数据封装的对象
     * @return
     */
    @RequestMapping("/servlet")
    public String servlet(HttpServletRequest request, HttpServletResponse response){
//    servlet 动态接收参数，所有的数据都是String数据类型
        String id =request.getParameter("id");
        int idInt = Integer.parseInt(id);
        String  name = request.getParameter("name");
//        SpringMVC将上述操作封装
        return  name;
    }
    /**
     * 根据ID查询数据
     */
    @GetMapping("getUserById")
    public User getUserById(Integer id){

        return userService.getUserById(id);
    }
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Integer id ){
        return userService.getUserById(id);
    }
    @DeleteMapping("user/{id}")
    public String deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return "数据删除成功";
    }
    @PostMapping("/addUser1")
    public String addUser1(@RequestBody User user){
        userService.addUser(user);
        return "添加成功";
    }
    @PostMapping("/addUserForm")
    public String addUserForm(User user){
        userService.addUserForm(user);
        return "表单提交成功";
    }
    @PostMapping("/user/{name}/{age}/{sex}")
    public String addUserRest(User user){
        userService.addUser(user);
        return "restFul入库成功";
    }
    @PutMapping("/user/{id}/{name}")
    public String updateUser(User user){
        userService.updateUser(user);
        return "修改成功";
    }
    @PutMapping("/updateUser2")
    public String updateUser2(@RequestBody User user){
        userService.updateUser(user);
        return "更新成功";
    }
}